package com.coderlin.service.impl;

import com.coderlin.entities.Customer;
import com.coderlin.mapper.CustomerMapper;
import com.coderlin.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/22 13:53 <br>
 * @see com.coderlin.service.impl <br>
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    /**
     * Service层接口用来数据访问
     *
     * @return:List<Customer></>
     */
    @Override
    public List<Customer> listCustomer() {
        return customerMapper.listCustomer();
    }

    /**
     * @param userName
     * @return:List<Customer></>
     */
    @Override
    public List<Customer> searchCustomer(String userName) {
        return customerMapper.searchCustomer(userName);
    }

    /**
     * 根据id查找Customer
     *
     * @param cid
     * @return
     */
    @Override
    public Customer searchCustomerById(int cid) {
        return customerMapper.searchCustomerById(cid);
    }

    /**
     * 添加用户
     *
     * @param customer
     * @return
     */
    @Override
    public Integer addCustomer(Customer customer) {
        return customerMapper.addCustomer(customer);
    }

    /**
     * 根据id删除
     *
     * @param cid
     * @return
     */
    @Override
    public Integer delCustomerById(int cid) {
        return customerMapper.delCustomerById(cid);
    }

    /**
     * update数据
     *
     * @param customer
     * @return
     */
    @Override
    public Integer modCustomer(Customer customer) {
        return customerMapper.modCustomer(customer);
    }

    /**
     * 查询列表分页
     *
     * @param pageNum 当前页码
     * @return 分页对象
     */
    @Override
    public PageInfo<Customer> listCustomerForPage(Integer pageNum) {
        //PageHelper的核心，将分页元素告诉PageHelper
        PageHelper.startPage(pageNum,CustomerService.PAGE_SiZE);
        List<Customer> customers = this.listCustomer();
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);

        return pageInfo;
    }

    /**
     * 表单搜索分页
     *
     * @param pageNum  当前页码
     * @param custName 客户名称
     * @return 分页对象
     */
    @Override
    public PageInfo<Customer> searchCustomer(Integer pageNum, String custName) {
        PageHelper.startPage(pageNum,CustomerService.PAGE_SiZE);
        List<Customer> customers = this.searchCustomer(custName);
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);

        return pageInfo;
    }
}
